require('../../assets/css/index.js')

require('./regiter.less')

let CaptchaMini = require("captcha-mini")


let dom = require('../../utils/dom')
let http = require('../../utils/http')
let valdator = require('../../utils/valdator')

dom.ready(function () {
    let captcha1 = new CaptchaMini();
    let yzm
    captcha1.draw(document.querySelector('#captcha1'), r => {
        yzm = r;
    });

    let Acc = dom.getEl("#account")
    let Sav = dom.getEl('#sav')
    let Pwd = dom.getEl('#pwd')
    let Surpwd = dom.getEl('#surpwd')
    Acc.addEventListener('blur', function () {
        let accval = Acc.value
        if (!accval) {
            weui.topTips("请输入手机号");
        } else if (!valdator.checkAcc(accval)) {
            weui.topTips("手机号验证不通过");
        } else {
            http.post('/api/user/isExist', {
                account: accval
            }, function (res) {
                console.log(res);
                if (res.errno == 0) {
                    weui.topTips("可以使用该手机号码");
                } else {
                    weui.topTips(res.message);
                }
            })
        }
    })


    Pwd.addEventListener('blur', function () {
        let Pwdval = Pwd.value
        if (!Pwdval) {
            weui.topTips("请输入密码");
        } else if (!valdator.checkPwd(Pwdval)) {
            weui.topTips("密码输入不符合");
        }
    })


    dom.getEl('#btn').addEventListener('click',function(){
        let Accval = Acc.value
        let Savval = Sav.value
        let Pwdval = Pwd.value
        let Surpwdval = Surpwd.value

        if(!Accval){
            weui.topTips('请输入手机号码')
        }else if(!valdator.checkAcc(Accval)){
            weui.topTips("手机号验证不通过");
        }else if(!Savval){
            weui.topTips('验证码不能为空')
        }else if(Savval.toLowerCase()!=yzm.toLowerCase()){
            weui.topTips('验证码不正确')
        }else if(!Pwdval){
            weui.topTips('密码不能为空')
        }else if(!valdator.checkPwd(Pwdval)){
            weui.topTips('密码输入不符合')
        }else if (Surpwdval!=Pwdval){
            weui.topTips('确认密码不一致')
        }else{
            http.post('/api/user/register',{
                account:Accval,
                password:Pwdval
            },function(res){
                if(res.errno==0){
                    weui.topTips('注册成功了！')
                    window.setTimeout(function(){
                        window.location.href='/login.html'
                    },2000) 
                }else{
                    weui.topTips(reg.message)
                }
            })
        }
    })
})